//
//  ZYVisitorView.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/11.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit
import SnapKit


class ZYVisitorView: UIView {

    // 闭包回调
    var callBackClosure: (()->())?
    
    
    // MARK: -懒加载控件 旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon") )
    // 罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon") )
    // 中心图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage (named: "visitordiscover_feed_image_house"))
    // 文本信息
    private lazy var messageLable: UILabel = {
        let lable = UILabel()
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(14)
        lable.text = "关注一些人，回这里看看有什么惊喜"
        lable.textAlignment = .Center
        lable.numberOfLines = 0
        return lable
    }()
    // 注册按钮
    private lazy var registerBtn:UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal
        )
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        return btn
    }()
    // 登录按钮
    private lazy var loginBtn:UIButton = {
        let btn = UIButton()
           btn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal
        )
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLable)
        addSubview(registerBtn)
        addSubview(loginBtn)
      // snapKit设置约束
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.size.equalTo(CGSize(width: 224, height: 40))
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLable.snp_bottom).offset(10)
            make.leading.equalTo(messageLable)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLable)
            make.top.equalTo(messageLable.snp_bottom).offset(10)
            make.size.equalTo(registerBtn)
        }
        
        self.startAnimation()
        
    }
    
    
    private func test() {
        // xcode自带设置约束方法
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false

        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
    }
    
    // 按钮的点击事件
   @objc private func registerBtnClick() {
//        print("register")
        callBackClosure?()
    }
  @objc private func loginBtnClick() {
//        print("login")
        callBackClosure?()
    }
    
    // MARK: -图片旋转
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        // 目的地
        animation.toValue = 2 * M_PI
        // 时长
        animation.duration = 20
        // 重复次数
        animation.repeatCount = MAXFLOAT
        // 防止动画释放掉
        animation.removedOnCompletion = false
        // 添加动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    // 设置访客界面
    func setVisitorInfo(title: String?, imageName:String?) {
        if  let t = title, name = imageName {
            
            messageLable.text = t
            iconImageView.image = UIImage(named: name)
            // 旋转图片隐藏
            cycleImageView.hidden = true
            
        } else {
            
            // 显示旋转图片
            cycleImageView.hidden = false
            // 开启旋转动画
            startAnimation()
        }

       
    }
    
}














